package conf

import (
	"encoding/json"
	"flag"
	"path/filepath"

	"imovie_collect/utils"
)

var mode string

func init() {
	flag.StringVar(&mode, "mode", "dev", "use config")
}

type ResourceConfig struct {
	Mongo map[string]*DbConfig
	Redis map[string]*DbConfig
	Mysql map[string]*DbConfig
	Kafka map[string]*DbConfig
	Es    map[string]*DbConfig
}

type DbConfig struct {
	Host string
	Port int
	Db   string
	User string
	Pwd  string
}

func LoadResource() *ResourceConfig {
	curDir := utils.CurDir()
	var f string
	if mode == "dev" {
		f = "json/resource.json"
	} else {
		f = "json_" + mode + "/resource.json"
	}
	f = filepath.Join(curDir, f)
	s, err := utils.ExtendFile(f)
	if err != nil {
		panic(err)
	}

	resource := new(ResourceConfig)
	err = json.Unmarshal([]byte(s), resource)
	if err != nil {
		panic(err)
	}
	return resource
}
